/*
 * Created by JFormDesigner on Wed May 19 14:31:25 CST 2021
 */

package com.dfrz.bookstore.view;

import java.awt.event.*;

import com.dfrz.bookstore.entry.Admin;
import com.dfrz.bookstore.entry.Book;
import com.dfrz.bookstore.service.BookService;
import com.dfrz.bookstore.service.impl.BookServiceImpl;
import com.dfrz.bookstore.util.OssTest;

import java.awt.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class addbook extends JFrame {
    Book book;
    /*测试更新*/
    public static void main(String[] args) {
//        UpDateBookInfo upDateBookInfo = new UpDateBookInfo(1);
//        upDateBookInfo.setVisible(true);
        addbook addBook = new addbook();
        addBook.setVisible(true);
    }
    /*无参构造表示添加图书*/
    public addbook() {
        initComponents2();
        window_flag.setText("添加图书");
    }
    /*有参构造表示修改图书信息*/
    public addbook(Book book) {
        this.book =book;
        initComponents2();
        window_flag.setText("更新图书");
        /*回显*/
        name.setText(book.getName());
        author.setText(book.getAuthor());
        publisher.setText(book.getPublisher());
        img_src.setText(book.getImg_src());
        price.setText(String.valueOf(book.getPrice()));
        type_id.setText(String.valueOf(book.getType_id()));
        discount.setText(String.valueOf(book.getDiscount()));
        store.setText(String.valueOf(book.getStore()));
        ID.setText(String.valueOf(book.getId())); //隐藏控件存储id 更新的时候需要id作为判断条件

        /*设置光碟复选框选中*/
        if (book.getDisc()==0){
            disc_false.setSelected(true);
        }else if(book.getDisc()==1) {
            disc_true.setSelected(true);
        }

        /*设置是否上架选中*/
        if (book.getFlag()==0){
            flag_false.setSelected(true);
        }else if(book.getFlag()==1){
            flag_true.setSelected(true);
        }

    }

    private void button2ActionPerformed(ActionEvent e) {
          Book book = new Book();
          book.setName(name.getText());
          book.setAuthor(author.getText());
          book.setPublisher(publisher.getText());

          //如果上传图书图片 即上传图片至OSS

          String bookImageUrl = null;
          if (img_src.getText()!=null){
              if (!img_src.getText().contains("http")){
                  bookImageUrl  = OssTest.sendToOss(img_src.getText());
                  book.setImg_src(bookImageUrl); //把返回的url存入对象中
              }
          }

          book.setPrice(Double.valueOf(price.getText()));
          book.setType_id(Integer.valueOf(type_id.getText()));



          /*判断折扣输入框是否有值*/
          if (!"".equals(discount.getText())){
              book.setDiscount(Double.valueOf(discount.getText()));
          }
          book.setStore(Integer.valueOf(store.getText()));
        /*判断id标签是否有值*/
          if (!"".equals(ID.getText())){
              book.setId(Integer.valueOf(ID.getText()));
          }
          /*判断是否有光碟的按钮选中情况*/
          if (disc_true.isSelected()){
              book.setDisc(1);
          }else if(disc_false.isSelected()){
              book.setDisc(0);
          }
        /*判断是否有上架的按钮选中情况*/
          if (flag_true.isSelected()){
              book.setFlag(1);
          }else if(flag_false.isSelected()){
              book.setFlag(0);
          }
          BookServiceImpl bookService = new BookServiceImpl();

          if("更新图书".equals(window_flag.getText())){
              /*调用图书service  更新图书*/
              bookService.upDataBook(book);

          }else if ("添加图书".equals(window_flag.getText())){
                //调用 添加图书
              bookService.inertBook(book);

          }
        dispose();
        new BookInfo(book,new Admin()).setVisible(true);

    }

    private void initComponents2() {
        panel1 = new JPanel();
        label1 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        label10 = new JLabel();
        label11 = new JLabel();
        flag_true = new JRadioButton();
        flag_false = new JRadioButton();
        disc_true = new JRadioButton();
        disc_false = new JRadioButton();
        name = new JTextField();
        author = new JTextField();
        publisher = new JTextField();
        img_src = new JTextField();
        price = new JTextField();
        type_id = new JTextField();
        discount = new JTextField();
        store = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        ID = new JLabel();
        window_flag = new JLabel();
        selectImg = new JButton();
        bookImg = new JLabel();

        //======== this ========
        setTitle("\u7ba1\u7406\u5458\u7cfb\u7edf");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- label1 ----
            label1.setText("\u4e66\u540d");
            panel1.add(label1);
            label1.setBounds(27, 40, 65, 50);

            //---- label3 ----
            label3.setText("\u4f5c\u8005");
            panel1.add(label3);
            label3.setBounds(25, 110, 65, 50);

            //---- label4 ----
            label4.setText("\u51fa\u7248\u793e");
            panel1.add(label4);
            label4.setBounds(20, 180, 65, 50);

            //---- label5 ----
            label5.setText("\u56fe\u7247\u94fe\u63a5");
            panel1.add(label5);
            label5.setBounds(10, 255, 65, 50);

            //---- label6 ----
            label6.setText("\u4ef7\u683c");
            panel1.add(label6);
            label6.setBounds(285, 40, 65, 50);

            //---- label7 ----
            label7.setText("\u4e66\u672c\u7c7b\u578b");
            panel1.add(label7);
            label7.setBounds(285, 100, 65, 50);

            //---- label8 ----
            label8.setText("\u6709\u65e0\u5149\u789f");
            panel1.add(label8);
            label8.setBounds(285, 150, 65, 50);

            //---- label9 ----
            label9.setText("\u6298\u6263");
            panel1.add(label9);
            label9.setBounds(300, 205, 65, 50);

            //---- label10 ----
            label10.setText("\u5e93\u5b58");
            panel1.add(label10);
            label10.setBounds(300, 260, 65, 50);

            //---- label11 ----
            label11.setText("\u662f\u5426\u4e0a\u67b6");
            panel1.add(label11);
            label11.setBounds(530, 35, 65, 50);

            //---- flag_true ----
            flag_true.setText("\u4e0a\u67b6");
            flag_true.setSelected(true);
            panel1.add(flag_true);
            flag_true.setBounds(590, 50, 60, flag_true.getPreferredSize().height);

            //---- flag_false ----
            flag_false.setText("\u4e0b\u67b6");
            panel1.add(flag_false);
            flag_false.setBounds(650, 50, 70, 19);

            //---- disc_true ----
            disc_true.setText("\u6709");
            panel1.add(disc_true);
            disc_true.setBounds(new Rectangle(new Point(355, 165), disc_true.getPreferredSize()));

            //---- disc_false ----
            disc_false.setText("\u65e0");
            disc_false.setSelected(true);
            panel1.add(disc_false);
            disc_false.setBounds(410, 165, 47, 19);
            panel1.add(name);
            name.setBounds(90, 55, 155, name.getPreferredSize().height);
            panel1.add(author);
            author.setBounds(90, 125, 155, 30);
            panel1.add(publisher);
            publisher.setBounds(90, 200, 155, 30);
            panel1.add(img_src);
            img_src.setBounds(90, 270, 125, 30);
            panel1.add(price);
            price.setBounds(350, 55, 80, 30);
            panel1.add(type_id);
            type_id.setBounds(350, 110, 115, 30);
            panel1.add(discount);
            discount.setBounds(350, 215, 75, 30);
            panel1.add(store);
            store.setBounds(350, 273, 70, 30);

            //---- button1 ----
            button1.setText("\u53d6\u6d88");
            panel1.add(button1);
            button1.setBounds(105, 375, 105, button1.getPreferredSize().height);

            //---- button2 ----
            button2.setText("\u63d0\u4ea4");
            button2.addActionListener(e -> button2ActionPerformed(e));
            panel1.add(button2);
            button2.setBounds(445, 375, 105, 30);

            //---- ID ----
            ID.setVisible(false);
            panel1.add(ID);
            ID.setBounds(36, 12, 40, 20);

            //---- window_flag ----
            window_flag.setText("\u5f53\u524d\u72b6\u6001");
            panel1.add(window_flag);
            window_flag.setBounds(325, 10, 60, window_flag.getPreferredSize().height);

            //---- selectImg ----
            selectImg.setText("\u9009\u62e9\u56fe\u7247");
            selectImg.addActionListener(e -> selectImgActionPerformed(e));
            selectImg.setIcon(null);
            panel1.add(selectImg);
            selectImg.setBounds(575, 290, 60, selectImg.getPreferredSize().height);

           if (book!=null){
               ImageIcon icon = null;
               try {
                   icon = new ImageIcon(new URL(book.getImg_src()));
               } catch (MalformedURLException e) {
                   e.printStackTrace();
               }
               icon.setImage(icon.getImage().getScaledInstance(150,200,Image.SCALE_DEFAULT));
               bookImg.setIcon(icon);
           }

            panel1.add(bookImg);
            bookImg.setBounds(535, 85, 150, 200);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(5, 5, 750, 470);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //---- flag ----
        ButtonGroup flag = new ButtonGroup();
        flag.add(flag_true);
        flag.add(flag_false);

        //---- disc ----
        ButtonGroup disc = new ButtonGroup();
        disc.add(disc_true);
        disc.add(disc_false);
    }

    /**
     * 选择图片
     * @param e
     */
    private void selectImgActionPerformed(ActionEvent e) {
        JFileChooser fd = new JFileChooser();
        fd.setFileSelectionMode(JFileChooser.OPEN_DIALOG);
        fd.showOpenDialog(null);
        File f = fd.getSelectedFile();
        if (f==null){
            return;
        }
        img_src.setText(f.getPath());
        ImageIcon icon = new ImageIcon(f.getPath());
        icon.setImage(icon.getImage().getScaledInstance(150,200,Image.SCALE_DEFAULT));
        bookImg.setIcon(icon);
        System.out.println("11");
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label1 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        label9 = new JLabel();
        label10 = new JLabel();
        label11 = new JLabel();
        flag_true = new JRadioButton();
        flag_false = new JRadioButton();
        disc_true = new JRadioButton();
        disc_false = new JRadioButton();
        name = new JTextField();
        author = new JTextField();
        publisher = new JTextField();
        img_src = new JTextField();
        price = new JTextField();
        type_id = new JTextField();
        discount = new JTextField();
        store = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        ID = new JLabel();
        window_flag = new JLabel();
        selectImg = new JButton();
        bookImg = new JLabel();

        //======== this ========
        setTitle("\u7ba1\u7406\u5458\u7cfb\u7edf");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- label1 ----
            label1.setText("\u4e66\u540d");
            panel1.add(label1);
            label1.setBounds(27, 40, 65, 50);

            //---- label3 ----
            label3.setText("\u4f5c\u8005");
            panel1.add(label3);
            label3.setBounds(25, 110, 65, 50);

            //---- label4 ----
            label4.setText("\u51fa\u7248\u793e");
            panel1.add(label4);
            label4.setBounds(20, 180, 65, 50);

            //---- label5 ----
            label5.setText("\u56fe\u7247\u94fe\u63a5");
            panel1.add(label5);
            label5.setBounds(10, 255, 65, 50);

            //---- label6 ----
            label6.setText("\u4ef7\u683c");
            panel1.add(label6);
            label6.setBounds(285, 40, 65, 50);

            //---- label7 ----
            label7.setText("\u4e66\u672c\u7c7b\u578b");
            panel1.add(label7);
            label7.setBounds(285, 100, 65, 50);

            //---- label8 ----
            label8.setText("\u6709\u65e0\u5149\u789f");
            panel1.add(label8);
            label8.setBounds(285, 150, 65, 50);

            //---- label9 ----
            label9.setText("\u6298\u6263");
            panel1.add(label9);
            label9.setBounds(300, 205, 65, 50);

            //---- label10 ----
            label10.setText("\u5e93\u5b58");
            panel1.add(label10);
            label10.setBounds(300, 260, 65, 50);

            //---- label11 ----
            label11.setText("\u662f\u5426\u4e0a\u67b6");
            panel1.add(label11);
            label11.setBounds(530, 35, 65, 50);

            //---- flag_true ----
            flag_true.setText("\u4e0a\u67b6");
            flag_true.setSelected(true);
            panel1.add(flag_true);
            flag_true.setBounds(590, 50, 60, flag_true.getPreferredSize().height);

            //---- flag_false ----
            flag_false.setText("\u4e0b\u67b6");
            panel1.add(flag_false);
            flag_false.setBounds(650, 50, 70, 19);

            //---- disc_true ----
            disc_true.setText("\u6709");
            panel1.add(disc_true);
            disc_true.setBounds(new Rectangle(new Point(355, 165), disc_true.getPreferredSize()));

            //---- disc_false ----
            disc_false.setText("\u65e0");
            disc_false.setSelected(true);
            panel1.add(disc_false);
            disc_false.setBounds(410, 165, 47, 19);
            panel1.add(name);
            name.setBounds(90, 55, 155, name.getPreferredSize().height);
            panel1.add(author);
            author.setBounds(90, 125, 155, 30);
            panel1.add(publisher);
            publisher.setBounds(90, 200, 155, 30);
            panel1.add(img_src);
            img_src.setBounds(90, 270, 125, 30);
            panel1.add(price);
            price.setBounds(350, 55, 80, 30);
            panel1.add(type_id);
            type_id.setBounds(350, 110, 115, 30);
            panel1.add(discount);
            discount.setBounds(350, 215, 75, 30);
            panel1.add(store);
            store.setBounds(350, 273, 70, 30);

            //---- button1 ----
            button1.setText("\u53d6\u6d88");
            panel1.add(button1);
            button1.setBounds(105, 375, 105, button1.getPreferredSize().height);

            //---- button2 ----
            button2.setText("\u63d0\u4ea4");
            button2.addActionListener(e -> button2ActionPerformed(e));
            panel1.add(button2);
            button2.setBounds(445, 375, 105, 30);

            //---- ID ----
            ID.setText("ID");
            ID.setVisible(false);
            panel1.add(ID);
            ID.setBounds(36, 12, 40, 20);

            //---- window_flag ----
            window_flag.setText("\u5f53\u524d\u72b6\u6001");
            panel1.add(window_flag);
            window_flag.setBounds(325, 10, 60, window_flag.getPreferredSize().height);

            //---- selectImg ----
            selectImg.setText("\u9009\u62e9\u56fe\u7247");
            selectImg.setIcon(null);
            panel1.add(selectImg);
            selectImg.setBounds(575, 290, 60, selectImg.getPreferredSize().height);
            panel1.add(bookImg);
            bookImg.setBounds(535, 85, 150, 200);

            { // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(5, 5, 750, 470);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //---- flag ----
        ButtonGroup flag = new ButtonGroup();
        flag.add(flag_true);
        flag.add(flag_false);

        //---- disc ----
        ButtonGroup disc = new ButtonGroup();
        disc.add(disc_true);
        disc.add(disc_false);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label1;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JLabel label10;
    private JLabel label11;
    private JRadioButton flag_true;
    private JRadioButton flag_false;
    private JRadioButton disc_true;
    private JRadioButton disc_false;
    private JTextField name;
    private JTextField author;
    private JTextField publisher;
    private JTextField img_src;
    private JTextField price;
    private JTextField type_id;
    private JTextField discount;
    private JTextField store;
    private JButton button1;
    private JButton button2;
    private JLabel ID;
    private JLabel window_flag;
    private JButton selectImg;
    private JLabel bookImg;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
